package pnu.ssm.sbmother;

import pnu.ssm.http.HttpConnectorParent;
import pnu.ssm.key.Key;
import pnu.ssm.key.SBString;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class SettingTab extends Fragment implements Button.OnClickListener {

	public static final String TAG = "SettingTab";
	private static final int UPDATE_SETTING = 51;
	Context mContext;
	SharedPreferences pref;
	SharedPreferences.Editor editor;
	
	TextView plus_problem_edit;
	TextView max_game_edit;

	Button btn_tab_setting_update;
	Button btn_tab_setting_decide;
	
	ProgressDialog dialog;
	HttpConnectorParent httpConnector = HttpConnectorParent.getInstance();
	
	Handler settingHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE_SETTING:
				Bundle bundle = msg.getData();
				String extra_time = bundle.getString("extra_time");
				String limit_time = bundle.getString("limit_time");
				plus_problem_edit.setText(extra_time);
				max_game_edit.setText(limit_time);
				break;
			default:
				break;
			}
		};
	};

	public SettingTab(Context context) {
		mContext = context;
		dialog = new ProgressDialog(mContext);
		dialog.setMessage(SBString.WAIT);
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		
		pref = mContext.getSharedPreferences(Key.prefKey, Context.MODE_PRIVATE);
		editor = pref.edit();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tab_setting, null);
		
		plus_problem_edit = (EditText) view.findViewById(R.id.plus_problem_edit);
		max_game_edit = (EditText) view.findViewById(R.id.max_game_edit);
		
		btn_tab_setting_update = (Button) view
				.findViewById(R.id.btn_tab_setting_update);
		btn_tab_setting_decide = (Button) view
				.findViewById(R.id.btn_tab_setting_decide);
		btn_tab_setting_update.setOnClickListener(this);
		btn_tab_setting_decide.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_tab_setting_update:
			new SettingUpdateAsyncTask().execute(null,null,null);
			break;
		case R.id.btn_tab_setting_decide:
			String extra_time = plus_problem_edit.getText().toString();
			String limit_time = max_game_edit.getText().toString();
			new SettingDecideAsyncTask(extra_time, limit_time).execute(null,null,null);
			break;
		default:
			Log.e(TAG, "default error");
			break;
		}
	}
	class SettingUpdateAsyncTask extends AsyncTask<Void, Void, Void>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.show();
		}
		protected Void doInBackground(Void... arg0) {
			String child_num = pref.getString(Key.child_num, "");
			String[] result = httpConnector.getChineseSetting(child_num);
			String extra_time = result[Key.EXTRA_TIME];
			String limit_time = result[Key.LIMIT_TIME];
			
			Message msg = Message.obtain();
			msg.what = UPDATE_SETTING;
			Bundle bundle = new Bundle();
			bundle.putString("extra_time", extra_time);
			bundle.putString("limit_time", limit_time);
			msg.setData(bundle);
			
			settingHandler.sendMessage(msg);
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			dialog.dismiss();
		}
	}
	class SettingDecideAsyncTask extends AsyncTask<Void, Void, Void>{
		String extra_time;
		String limit_time;
		public SettingDecideAsyncTask(String extra_time, String limit_time) {
			this.extra_time = extra_time;
			this.limit_time = limit_time;
		}
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.show();
		}
		protected Void doInBackground(Void... arg0) {
			String child_num = pref.getString(Key.child_num, "");
			httpConnector.sendChineseSetting(child_num, extra_time,limit_time);
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			dialog.dismiss();
		}
	}

}
